package com.lizonghui.googleplay.fragment;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.lizonghui.googleplay.base.BaseFragment;
import com.lizonghui.googleplay.base.LoadingPager;
import com.lizonghui.googleplay.protocol.HotProtocol;
import com.lizonghui.googleplay.utils.UIUtils;
import com.lizonghui.googleplay.views.FlowLayout;

import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2016/6/16.
 */
public class HotFragment extends BaseFragment {

    private List<String> mList;

    @Override
    public View initSuccessView() {

        ScrollView sv = new ScrollView(UIUtils.getContext());//实现滚动效果
        FlowLayout fl = new FlowLayout(UIUtils.getContext());

        //添加孩子
        for (final String data : mList) {
            TextView tv = new TextView(UIUtils.getContext());
            tv.setTextColor(Color.GREEN);
            tv.setTextSize(30);
            tv.setText(data);

//            tv.setBackgroundResource(R.drawable.shape_hot_tv);
            //设置padding
            int padding = UIUtils.dip2px(4);
            tv.setPadding(padding, padding, padding, padding);
            //设置居中
            tv.setGravity(Gravity.CENTER);

            //用代码方式创建shape图片
            GradientDrawable normalBg = new GradientDrawable();
            normalBg.setCornerRadius(8);
            Random random = new Random();
            int alpha = 255;
            int red = random.nextInt(190) + 30;//获得30 -220
            int greed = random.nextInt(190) + 30;
            int blue = random.nextInt(190) + 30;
            int argb = Color.argb(alpha, red, greed, blue);//设置颜色
            normalBg.setColor(argb);
//            tv.setBackgroundDrawable(normalBg);

            GradientDrawable pressBg = new GradientDrawable();
            pressBg.setCornerRadius(8);
            pressBg.setColor(Color.DKGRAY);


            //用代码创建一个selector，注意顺序不能变
            StateListDrawable selectorBg = new StateListDrawable();
            selectorBg.addState(new int[]{android.R.attr.state_pressed}, pressBg);
            selectorBg.addState(new int[]{}, normalBg);

            tv.setBackgroundDrawable(selectorBg);

            //设置TV可以点击,selector效果才会有
            tv.setClickable(true);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(UIUtils.getContext(), data, Toast.LENGTH_SHORT).show();
                }
            });

            fl.addView(tv);
        }

        sv.addView(fl);
        return sv;
    }

    @Override
    public LoadingPager.LoadedResultState initData() {
        SystemClock.sleep(2000);// 模拟网络耗时2s种
        //得到protocol对象
        HotProtocol hotProtocol = new HotProtocol();
        try {
            //用对象去请求数据
            mList = hotProtocol.loadData(0);
            return checkResData(mList);
        } catch (IOException e) {
            e.printStackTrace();
            return LoadingPager.LoadedResultState.ERROR;
        }
    }
}
